package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import com.wxw.BaseLibrary.element.IListBoxItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2014-07-11.
 */
public class ListBox<T extends IListBoxItem> extends ScrollView {
    public static final int CAMERA_RESULT_CODE = 110;
    public static final int ALBUM_RESULT_CODE = 111;
    protected LinearLayout linearLayout;
    protected float itemFontSize = 18;
    protected int captionWidth = 200;
    protected int vMarginSize = 3;
    protected int hMarginSize = 1;
    protected boolean editable = false;
    protected int bgColor = Color.parseColor("#c7c7c7");
    protected int itemChangedBgColor = Color.YELLOW;
    protected List<T> items = new ArrayList<T>();
    protected int iSpinnerDropDownIconID = 0;

    private OnListBoxItemChangedListener listBoxItemChangedListener;

    public ListBox(Context context) {
        super(context);
        setBackgroundColor(bgColor);
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(bgColor);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(5,5,5,5);
        addView(linearLayout, layoutParams);
    }

    public float getItemFontSize() {
        return itemFontSize;
    }

    public void setItemFontSize(float itemFontSize) {
        this.itemFontSize = itemFontSize;
    }

    public int getCaptionWidth() {
        return captionWidth;
    }

    public void setCaptionWidth(int captionWidth) {
        this.captionWidth = captionWidth;
    }

    public int getVMarginSize() {
        return vMarginSize;
    }

    public void setVMarginSize(int vMarginSize) {
        this.vMarginSize = vMarginSize;
    }

    public int getHMarginSize() {
        return hMarginSize;
    }

    public void setHMarginSize(int hMarginSize) {
        this.hMarginSize = hMarginSize;
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        setBackgroundColor(bgColor);
        linearLayout.setBackgroundColor(bgColor);
    }

    public int getItemChangedBgColor() {
        return itemChangedBgColor;
    }

    public void setItemChangedBgColor(int itemChangedBgColor) {
        this.itemChangedBgColor = itemChangedBgColor;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public int getSpinnerDropDownIconID() {
        return iSpinnerDropDownIconID;
    }

    public void setSpinnerDropDownIconID(int iSpinnerDropDownIconID) {
        this.iSpinnerDropDownIconID = iSpinnerDropDownIconID;
    }

    public List<T> getItems(){
        return items;
    }

    public void setListBoxItemChangedListener(OnListBoxItemChangedListener listBoxItemChangedListener) {
        this.listBoxItemChangedListener = listBoxItemChangedListener;
    }

    public void addItems(List<T> items) {
        this.items = items;
        refreshView();
    }

    public void addItem(T item){
        items.add(item);
        addView(item);
    }

    public void refreshView(){
        if (items==null || items.size()<=0) {
            return;
        }
        linearLayout.removeAllViews();
        for (IListBoxItem item : items) {
            addView(item);
        }
    }

    private void addView(IListBoxItem item){
//        item.setOnListBoxItemChangedListener(listBoxItemChangedListener);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(hMarginSize,vMarginSize,hMarginSize,vMarginSize);
        linearLayout.addView(item.getView(), layoutParams);

//        View view = item.getView();
//        view.setTag(item);
//        view.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (view.getTag()!=null && view.getTag() instanceof IListBoxItem) {
//                    if (listBoxItemChangedListener!=null)
//                        listBoxItemChangedListener.onItemChanged((IListBoxItem)view.getTag(), ListBox.this);
//                }
//            }
//        });
//        linearLayout.addView(view, layoutParams);
    }

    public interface OnListBoxItemChangedListener{
        void onItemChanged(IListBoxItem item, Object value);
    }

}
